<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring Bean 生命周期管理</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .code-block {
            background: #1e1e1e;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
            position: relative;
            overflow: hidden;
        }
        .code-block::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 40px;
            background: #2d2d2d;
            border-bottom: 1px solid #3e3e3e;
        }
        .code-dots {
            position: absolute;
            top: 14px;
            left: 20px;
            display: flex;
            gap: 8px;
        }
        .code-dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        .annotation-card {
            background: white;
            border-radius: 16px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            transition: all 0.3s ease;
            border: 1px solid rgba(0,0,0,0.05);
        }
        .annotation-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0,0,0,0.15);
        }
        .feature-icon {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        pre code {
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            font-size: 14px;
            line-height: 1.6;
            color: #d4d4d4;
            padding-top: 60px;
            display: block;
            padding-left: 20px;
            padding-right: 20px;
            padding-bottom: 20px;
        }
        .keyword { color: #569cd6; }
        .annotation { color: #dcdcaa; }
        .string { color: #ce9178; }
        .comment { color: #6a9955; }
        .method { color: #dcdcaa; }
        .class-name { color: #4ec9b0; }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6 text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 tracking-tight">
                Spring Bean 生命周期管理
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90 max-w-3xl mx-auto">
                深入理解 @PostConstruct 和 @PreDestroy 注解的使用与最佳实践
            </p>
            <div class="flex justify-center gap-4 text-sm">
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full">
                    <i class="fas fa-code mr-2"></i>Spring Framework
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full">
                    <i class="fas fa-cog mr-2"></i>生命周期管理
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full">
                    <i class="fas fa-rocket mr-2"></i>最佳实践
                </span>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-12 max-w-6xl">
        <!-- Introduction -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12">
            <p class="text-lg leading-relaxed text-gray-700 indent-8">
                <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded font-mono text-base">@PostConstruct</code> 和 
                <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded font-mono text-base">@PreDestroy</code> 
                是 Java 中用于管理 Bean 生命周期的注解，通常用于 Spring 框架中来控制 Bean 的初始化和销毁过程。这两个注解提供了一种机制，可以在 Bean 的创建和销毁时执行特定的操作。
            </p>
        </div>

        <!-- Lifecycle Diagram -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12">
            <h2 class="text-2xl font-bold mb-6 text-gray-800 flex items-center">
                <i class="fas fa-diagram-project mr-3 feature-icon"></i>
                Bean 生命周期流程
            </h2>
            <div class="mermaid">
                graph LR
                    A[Bean 实例化] --> B[依赖注入]
                    B --> C{@PostConstruct}
                    C --> D[Bean 就绪]
                    D --> E[使用中...]
                    E --> F{@PreDestroy}
                    F --> G[Bean 销毁]
                    
                    style A fill:#f9f,stroke:#333,stroke-width:2px
                    style C fill:#9f9,stroke:#333,stroke-width:2px
                    style F fill:#ff9,stroke:#333,stroke-width:2px
                    style G fill:#f99,stroke:#333,stroke-width:2px
            </div>
        </div>

        <!-- Annotations Grid -->
        <div class="grid md:grid-cols-2 gap-8 mb-12">
            <!-- @PostConstruct Card -->
            <div class="annotation-card p-8">
                <div class="flex items-center mb-6">
                    <div class="w-16 h-16 bg-green-100 rounded-full flex items-center justify-center mr-4">
                        <i class="fas fa-play-circle text-3xl text-green-600"></i>
                    </div>
                    <h3 class="text-2xl font-bold text-gray-800">@PostConstruct</h3>
                </div>
                
                <div class="space-y-4 mb-6">
                    <div class="flex items-start">
                        <i class="fas fa-bullseye text-purple-600 mt-1 mr-3"></i>
                        <div>
                            <h4 class="font-semibold text-gray-800 mb-1">用途</h4>
                            <p class="text-gray-600">标记一个方法，在依赖注入完成后、Bean 的初始化阶段之后自动调用。用于执行任何初始化任务。</p>
                        </div>
                    </div>
                    
                    <div class="flex items-start">
                        <i class="fas fa-lightbulb text-purple-600 mt-1 mr-3"></i>
                        <div>
                            <h4 class="font-semibold text-gray-800 mb-1">使用场景</h4>
                            <p class="text-gray-600">加载配置信息、初始化资源、检查依赖关系、建立数据库连接等。</p>
                        </div>
                    </div>
                </div>

                <div class="code-block">
                    <div class="code-dots">
                        <div class="code-dot bg-red-500"></div>
                        <div class="code-dot bg-yellow-500"></div>
                        <div class="code-dot bg-green-500"></div>
                    </div>
                    <pre><code><span class="keyword">import</span> javax.annotation.PostConstruct;

<span class="annotation">@Component</span>
<span class="keyword">public class</span> <span class="class-name">MyBean</span> {

    <span class="annotation">@PostConstruct</span>
    <span class="keyword">public void</span> <span class="method">init</span>() {
        <span class="comment">// 在依赖注入完成后执行初始化操作</span>
        System.out.println(<span class="string">"Bean 初始化完成"</span>);
    }
}</code></pre>
                </div>
            </div>

            <!-- @PreDestroy Card -->
            <div class="annotation-card p-8">
                <div class="flex items-center mb-6">
                    <div class="w-16 h-16 bg-red-100 rounded-full flex items-center justify-center mr-4">
                        <i class="fas fa-stop-circle text-3xl text-red-600"></i>
                    </div>
                    <h3 class="text-2xl font-bold text-gray-800">@PreDestroy</h3>
                </div>
                
                <div class="space-y-4 mb-6">
                    <div class="flex items-start">
                        <i class="fas fa-bullseye text-purple-600 mt-1 mr-3"></i>
                        <div>
                            <h4 class="font-semibold text-gray-800 mb-1">用途</h4>
                            <p class="text-gray-600">标记一个方法，在 Bean 被销毁之前自动调用。用于执行任何清理任务。</p>
                        </div>
                    </div>
                    
                    <div class="flex items-start">
                        <i class="fas fa-lightbulb text-purple-600 mt-1 mr-3"></i>
                        <div>
                            <h4 class="font-semibold text-gray-800 mb-1">使用场景</h4>
                            <p class="text-gray-600">释放资源、关闭连接、保存状态、清理临时文件等。</p>
                        </div>
                    </div>
                </div>

                <div class="code-block">
                    <div class="code-dots">
                        <div class="code-dot bg-red-500"></div>
                        <div class="code-dot bg-yellow-500"></div>
                        <div class="code-dot bg-green-500"></div>
                    </div>
                    <pre><code><span class="keyword">import</span> javax.annotation.PreDestroy;

<span class="annotation">@Component</span>
<span class="keyword">public class</span> <span class="class-name">MyBean</span> {

    <span class="annotation">@PreDestroy</span>
    <span class="keyword">public void</span> <span class="method">cleanup</span>() {
        <span class="comment">// 在 Bean 被销毁之前执行清理操作</span>
        System.out.println(<span class="string">"Bean 即将被销毁"</span>);
    }
}</code></pre>
                </div>
            </div>
        </div>

        <!-- Best Practices -->
        <div class="bg-gradient-to-r from-purple-50 to-pink-50 rounded-2xl p-8 mb-12">
            <h2 class="text-2xl font-bold mb-6 text-gray-800 flex items-center">
                <i class="fas fa-star mr-3 text-yellow-500"></i>
                最佳实践建议
            </h2>
            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-white rounded-lg p-6 shadow-md">
                    <h3 class="font-semibold text-lg mb-3 text-purple-700">
                        <i class="fas fa-check-circle mr-2"></i>推荐做法
                    </h3>
                    <ul class="space-y-2 text-gray-700">
                        <li><i class="fas fa-angle-right text-purple-500 mr-2"></i>方法应该是无参的</li>
                        <li><i class="fas fa-angle-right text-purple-500 mr-2"></i>方法不应该抛出受检异常</li>
                        <li><i class="fas fa-angle-right text-purple-500 mr-2"></i>保持方法简洁，避免复杂逻辑</li>
                        <li><i class="fas fa-angle-right text-purple-500 mr-2"></i>确保操作的幂等性</li>
                    </ul>
                </div>
                <div class="bg-white rounded-lg p-6 shadow-md">
                    <h3 class="font-semibold text-lg mb-3 text-red-700">
                        <i class="fas fa-times-circle mr-2"></i>避免事项
                    </h3>
                    <ul class="space-y-2 text-gray-700">
                        <li><i class="fas fa-angle-right text-red-500 mr-2"></i>不要在方法中进行长时间操作</li>
                        <li><i class="fas fa-angle-right text-red-500 mr-2"></i>避免依赖其他 Bean 的状态</li>
                        <li><i class="fas fa-angle-right text-red-500 mr-2"></i>不要在销毁方法中创建新资源</li>
                        <li><i class="fas fa-angle-right text-red-500 mr-2"></i>避免循环依赖问题</li>
                    </ul>
                